package com.platform.service.impl;

import com.platform.dao.MerchantInformationDao;
import com.platform.dao.UserAssetDao;
import com.platform.entity.MerchantInfomationEntity;
import com.platform.entity.UserAssetEntity;
import com.platform.entity.excel.CashOutExcel;
import com.platform.service.UserAssetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.platform.dao.CashOutDao;
import com.platform.entity.CashOutEntity;
import com.platform.service.CashOutService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 提现申请表Service实现类
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2019-02-17 10:44:13
 */
@Service("cashOutService")
public class CashOutServiceImpl implements CashOutService {
    @Autowired
    private CashOutDao cashOutDao;
    @Autowired
    private UserAssetDao userAssetDao;
    @Autowired
    private MerchantInformationDao merchantInformationDao;

    @Override
    public CashOutEntity queryObject(Long id) {
        return cashOutDao.queryObject(id);
    }

    @Override
    public List<CashOutEntity> queryList(Map<String, Object> map) {
        return cashOutDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return cashOutDao.queryTotal(map);
    }

    @Override
    public int save(CashOutEntity tCashOut) {
        return cashOutDao.save(tCashOut);
    }

    @Transactional
    @Override
    public int update(CashOutEntity tCashOut) {
        long uid =tCashOut.getUid();
        UserAssetEntity asset = userAssetDao.queryObjectForUid(uid);
        if(tCashOut.getStatus()==3){
            BigDecimal frozen = asset.getFrozen().subtract(tCashOut.getAmount());
            asset.setFrozen(frozen);
            int result = userAssetDao.updateAsset(asset);
            if(result<=0){
                return 0;
            }
        }
        tCashOut.setRealAmount(tCashOut.getAmount());
        return cashOutDao.update(tCashOut);
    }
    @Transactional
    @Override
    public int finish(CashOutEntity tCashOut) {
        long uid =tCashOut.getUid();
        UserAssetEntity asset = userAssetDao.queryObjectForUid(uid);
        BigDecimal money =BigDecimal.ZERO;
        BigDecimal frozen =BigDecimal.ZERO;
        if(tCashOut.getStatus()==4){
             money = asset.getAmount().subtract(tCashOut.getAmount());
             frozen =asset.getFrozen().subtract(tCashOut.getAmount());
        }else {
            money = asset.getAmount();
            frozen = asset.getFrozen().subtract(tCashOut.getAmount());
        }

        if(money.compareTo(BigDecimal.ZERO)<0||frozen.compareTo(BigDecimal.ZERO)<0){
            return 0;
        }
        asset.setAmount(money);
        asset.setFrozen(frozen);
       int re =  userAssetDao.update(asset);
       if(re<=0){
           return 0;
       }
        return cashOutDao.update(tCashOut);
    }

    @Override
    public int delete(Long id) {
        return cashOutDao.delete(id);
    }

    @Override
    public int deleteBatch(Long[] ids) {
        return cashOutDao.deleteBatch(ids);
    }

    @Override
    public List<CashOutExcel> queryListExcel(String name, String phone, String cardNo, Integer status, Integer index, Integer size) {
        int start=(index-1)*size;
        return cashOutDao.queryListExcel( name,  phone,  cardNo,  status,  start,  size);
    }

    /**
     * 提现完成
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean success(Integer id, Integer status, Integer version, MerchantInfomationEntity merchantInfomationEntity) throws Exception {
        CashOutEntity cashOutEntity = cashOutDao.queryObject(id);
        Map<String,Object> param = new HashMap<String, Object>();
        param.put("id",id);
        param.put("status",status);
        param.put("version",version);
        if(cashOutDao.success(param)==false){
            return false;
        }
        param = new HashMap<String, Object>();
        param.put("money",cashOutEntity.getAmount());
        param.put("version",merchantInfomationEntity.getVersion());
        param.put("mid",merchantInfomationEntity.getMid());
        Integer update = merchantInformationDao.cashOutSuccess(param);
        if(update<=0){
            throw new Exception();
        }
        return true;
    }


    /**
     * 提现完成
     * @param status
     * @param id
     * @param version
     * @return
     */
    @Override
    public Integer updateCashOutStatus(Integer status, Integer id,Integer version) {
        Map<String,Object> param = new HashMap<String, Object>();
        param.put("id",id);
        param.put("status",status);
        param.put("version",version);
        return cashOutDao.updateCashOutStatus(param);
    }

    /**
     * 提现审核失败
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean cashOutFail(CashOutEntity cashOutEntity,MerchantInfomationEntity merchantInfomationEntity) throws Exception{
        Map<String,Object> param = new HashMap<String, Object>();
        param.put("id",cashOutEntity.getId());
        param.put("status",cashOutEntity.getStatus());
        param.put("version",cashOutEntity.getVersion());
        if(cashOutDao.updateCashOutStatus(param)<=0){
            return false;
        }
        param = new HashMap<String, Object>();
        param.put("money",cashOutEntity.getAmount());
        param.put("mid",merchantInfomationEntity.getMid());
        param.put("version",merchantInfomationEntity.getVersion());
        if(merchantInformationDao.cashOutFail(param)<=0){
            throw new Exception();
        }
        return true;
    }

    @Override
    public Map<String, Object> queryCashOutMap(Integer uid) {

        return cashOutDao.queryCashOutMap(uid);
    }


}
